<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button class="btn">随机数</button>
  <script>
    // 1.定义的数组
  const values = [100,300,500,800,1000]
  // 2.监听按钮点击
  const brnEl = document.querySelector(".btn")
  brnEl.onclick = function() {
    let currentValue = Math.floor(Math.random() * 1500)
    matchIndex(currentValue)
  }

  // 3.封装函数：根据value去匹配索引
  function matchIndex(currentValue) {
    let currentIndex = values.length -1
    for(let i =0; i<values.length;i++){
      const value = values[i]
      if(value > currentValue) {
        currentIndex = i-1
        break
      }
    }
    if (currentIndex === -1) return
    console.log("value:",currentValue,"index:",currentIndex);
  }
  </script>
</body>
</html>